package com.game.base.server.base;

import android.content.Context;

import com.game.base.app.App;
import com.game.base.utils.log.Logger;
import com.game.base.utils.time.FormatPattern;
import com.game.base.utils.time.TimeUtil;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobConfig;
import cn.bmob.v3.BmobInstallationManager;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * Bmob管理类
 * Created by Su on 2019/5/8.
 */
public class BmobManager {
    /**
     * Bmob初始化
     */
    public static void initBmob(Context context) {
        BmobConfig config = new BmobConfig.Builder(context)
                .setApplicationId("0689df853b9bfcdc43212de8dc0ffa72")
                //请求超时时间（单位为秒）：默认15s
                .setConnectTimeout(30)
                //文件分片上传时每片的大小（单位字节），默认512*1024
                .setUploadBlockSize(1024 * 1024)
                //文件的过期时间(单位为秒)：默认1800s
                .setFileExpiration(2500)
                .build();
        Bmob.initialize(config);
    }

    /**
     * 设备唯一标示
     */
    public static String getDeviceId() {
        return BmobInstallationManager.getInstallationId();
    }

    /**
     * 获取设备信息
     */
    public static void getDeviceInfo() {
        BmobInstallationManager.getInstance().getCurrentInstallation();
    }

    /**
     * 获取服务器时间
     */
    public static void getServerTime() {
        getServerTime(null);
    }

    public static void getServerTime(QueryListener<Long> listener) {
        Bmob.getServerTime(new QueryListener<Long>() {
            @Override
            public void done(Long aLong, BmobException e) {
                if (e == null) {
                    App.time = aLong * 1000L;
                    Logger.d("更新服务器时间：" + TimeUtil.formatTime(App.time, FormatPattern.FP_YMD_HMS));
                } else {
                    App.time = System.currentTimeMillis();
                    Logger.printThrowable(e);
                }
                if (listener != null) {
                    listener.done(aLong, e);
                }
            }
        });
    }
}
